/*******************************************************************************
 * Copyright 2011-2013 Sergey Tarasevich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.nostra13.example.universalimageloader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import com.android.customized.MyAdGallery;
import com.android.customized.MyAdGallery.MyOnItemClickListener;
import com.dxyxx.R;
import com.nostra13.example.universalimageloader.Constants.Extra;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class ImageListActivity extends AbsListViewBaseActivity {

	DisplayImageOptions options;

	String[] imageUrls;
	private MyAdGallery gallery; // 广告控件
	LinearLayout ovalLayout; // 圆点容器
	/** 图片id的数组,本地测试用 */
	private int[] imageId = new int[] { R.drawable.img01, R.drawable.img02,
			R.drawable.img03, R.drawable.img04 };
	/** 图片网络路径数组 */
	private String[] mris = {
			"http://img.my.csdn.net/uploads/201312/14/1386989803_3335.PNG",
			"http://img.my.csdn.net/uploads/201312/14/1386989613_6900.jpg",
			"http://img.my.csdn.net/uploads/201312/14/1386989802_7236.PNG" };
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_image_list);
		((ImageView) findViewById(R.id.backImageButton))
				.setVisibility(View.INVISIBLE);
		((TextView) findViewById(R.id.title)).setText("大象与猩猩");
		Bundle bundle = getIntent().getExtras();
//		LinearLayout ll = (LinearLayout) findViewById(R.id.imagesList);
//		ImageView[] images = new ImageView[4];
		// imageUrls = bundle.getStringArray(Extra.IMAGES);
		imageUrls = new String[] {
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",
				"http://pay.dxyxx.com:4869/d7d15915dafa177c13582b78367e6dec?p=0",

		};
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new ScaleBitmapDisplayer()).build();

		// for (int i = 0; i < 4; i++) {
		// images[i] = new ImageView(this);
		// LinearLayout.LayoutParams p = new
		// LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		// images[i].setLayoutParams(p);
		// ll.addView(images[i]);
		// imageLoader.displayImage(imageUrls[0], images[i],
		// options,animateFirstListener);
		//
		// }

		listView = (ListView) findViewById(android.R.id.list);
		HeaderViewListAdapter a = null;
		View headView = getLayoutInflater().inflate(R.layout.listheader, null);
		((ListView) listView).setAdapter(new ItemAdapter());
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				startImagePagerActivity(position);
			}
		});

		gallery = (MyAdGallery) findViewById(R.id.adgallery); // 获取Gallery组件
		ovalLayout = (LinearLayout) findViewById(R.id.ovalLayout);// 获取圆点容器
		// 第二和第三参数 2选1 ,参数2为 图片网络路径数组 ,参数3为图片id的数组,本地测试用 ,2个参数都有优先采用 参数2
		gallery.start(this, mris, imageId, 3000, ovalLayout,
				R.drawable.dot_focused, R.drawable.dot_normal);
		gallery.setMyOnItemClickListener(new MyOnItemClickListener() {
			public void onItemClick(int curIndex) {
				Toast.makeText(ImageListActivity.this, "点击的图片下标为:" + curIndex,
						Toast.LENGTH_SHORT).show();
				// System.out.println(curIndex);
			}
		});
	}

	@Override
	public void onBackPressed() {
		AnimateFirstDisplayListener.displayedImages.clear();
		super.onBackPressed();
	}

	private void startImagePagerActivity(int position) {
		Intent intent = new Intent(this, ImagePagerActivity.class);
		intent.putExtra(Extra.IMAGES, imageUrls);
		intent.putExtra(Extra.IMAGE_POSITION, position);
		startActivity(intent);
	}

	class ItemAdapter extends BaseAdapter {

		private class ViewHolder {
			public TextView text;
			public ImageView image;
		}

		@Override
		public int getCount() {
			return imageUrls.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			View view = convertView;
			final ViewHolder holder;
			if (convertView == null) {
				view = getLayoutInflater().inflate(R.layout.item_homepage_imageslist,
						parent, false);
				holder = new ViewHolder();
//				holder.text = (TextView) view.findViewById(R.id.text);
				holder.image = (ImageView) view.findViewById(R.id.image);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

//			holder.text.setText("Item " + (position + 1));

			imageLoader.displayImage(imageUrls[0], holder.image, options,
					animateFirstListener);

			return view;
		}
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}